import homeApi from "@/static/js/enterpriseServer/index.js"
export default {
	data() {
		return {
			tabIndex: 0,
			tabList: [{
					id: 1,
					name: "服务商精选",
				},
				{
					id: 0,
					name: "服务精选",
				},],
			shopList: [],
			serviceList: [],
			demandList: [],
		}
	},
	onShow() {
		this.getServiceList();
		this.getDemandList();
	},
	methods: {
		tabToggle(e) {
			this.tabIndex = e.index;
			if (e.index == 0) {
				this.getServiceList();
			} else {
				this.getShopList();
			}
		},

		//获取服务商列表
		getServiceList() {
			let params = {
				page: 1,
				pagesize: 6,
				is_recommend: 1,
			}
			homeApi.getServiceList(params).then(res => {
				if (res.code == 1) {
					let arr = res.data.list;
					this.serviceList = arr;
				}
			})
		},

		//服务精选
		getShopList() {
			let params = {
				page: 1,
				pagesize: 6,
				is_recommend: 1,
			}
			homeApi.getShopList(params).then(res => {
				if (res.code == 1) {
					let arr = res.data;
					arr.forEach(item => {
						item.pic = item.pic.split(',')[0];
					})
					this.shopList = arr;
				}
			})
		},

		more(type, url) {
			if (type == 1) {
				uni.navigateTo({
					url: url
				})
			} else if (type == 2) {
				let index = this.tabIndex == 0 ? 1 : 0;
				uni.navigateTo({
					url: url + '?index=' + index
				})
			}
		},


		//获取需求列表数据
		async getDemandList() {
			// uni.showLoading({
			// 	title: '加载中'
			// })
			let obj = {
				page: 1,
				pagesize: 3,
			}
			let res = await homeApi.getDemandNewList(obj)
			if (res.code === 1) {
				this.demandList = res.data.recommend.slice(0, 3)
				// uni.hideLoading()
			}
		},

		goTo(type, item, url) {
			let token = uni.getStorageSync('token');
			if (type == 1) {
				if (!token) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return false;
				}
			}
			uni.navigateTo({
				url: url + '?id=' + item.id
			})
		},
		server(type, url) {
			let token = uni.getStorageSync('token');
			if (type == 1) {
				if (!token) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return false;
				}
			}
			uni.navigateTo({
				url: url
			})
		},

		become() {
			let token = uni.getStorageSync('token');
			if (!token) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return false;
			}
			homeApi.getUserInfo().then(res => {
				if (res.code === 1) {
					let userInfo = res.data;
					if (userInfo.is_server == 2 || userInfo.is_server == 1) {
						uni.navigateTo({
							url: '/enterpriseServer/becomeServer/serverStarts'
						})
					} else {
						uni.navigateTo({
							url: '/enterpriseServer/becomeServer/index'
						})
					}
				}
			})
		},
	}
}